//
//  HWMainHaoView.m
//  HWanGame
//
//  Created by 陈道斌 on 2019/9/28.
//  Copyright © 2019 Chendaobin. All rights reserved.
//

#import "HWMainHaoView.h"
@interface HWMainHaoView ()

@property(strong,nonatomic) UIView *grayView;
@property(strong,nonatomic) UIView *fenxiangView;
@property(strong,nonatomic) UIView *chushouView;
@property(strong,nonatomic) UIButton *jizhubutt;


@end


@implementation HWMainHaoView

- (instancetype)initWithFrame:(CGRect)frame {
    
    if (self = [super initWithFrame:frame]) {
        [self addgrayView];
        
        [self setupUI];
        
        
        
        
    }
    
    return  self;
    
    
    
}


- (void)setupUI {
    
    
    self.fenxiangView = [[UIView alloc]initWithFrame:CGRectMake(39, (kScreenH-320)/2, kScreenW-39*2,320)];
    self.fenxiangView.layer.cornerRadius = 6;
    self.fenxiangView.layer.masksToBounds = YES;
    self.fenxiangView.backgroundColor =[UIColor whiteColor];
    [self addSubview:self.fenxiangView];
    // 卖家须知弹框
    [self addTheSellerNoticeView];
    
    
}

- (void)addTheSellerNoticeView {
    
    
    
    self.chushouView = [[UIView alloc]initWithFrame:CGRectMake(0, 0,  kScreenW-39*2, 300)];
    //self.chushouView.backgroundColor = [UIColor redColor];
    [self.fenxiangView addSubview:self.chushouView];
    
    [self addmaijiaxuezhshouhu];
    
    
    UIButton *jizhubutt  = [UIButton setButtonWithFrame:CGRectMake(21, self.chushouView.bottom, 200, 30) Title:@"我已阅读并同意《用户注册协议》" Image:@"weixuanzhon_bynenn" Font:12 Color:RGB(153, 153, 153) Target:self Selector:@selector(xieYiButtonTap:)];
    // jizhubutt.backgroundColor = [UIColor redColor];
    self.jizhubutt = jizhubutt;
    
    [jizhubutt setImage:[UIImage imageNamed:@"sign_sure"] forState:UIControlStateSelected];
    //  jizhubutt.backgroundColor = [UIColor redColor];
    NSString * aStr = @"我已阅读并同意《用户注册协议》";
    [jizhubutt layoutButtonWithEdgeInsetsStyle:MKButtonEdgeInsetsStyleLeft imageTitleSpace:4];
    NSMutableAttributedString *str = [WMUtil attributedStringWithText:aStr withRangText:@"《用户注册协议》" withColor:RGB(26, 113, 255) withFont:[UIFont systemFontOfSize:12]];
    [jizhubutt setAttributedTitle:str forState:UIControlStateNormal];
    [self.fenxiangView addSubview:jizhubutt];
    
    CGFloat jianju =  (kScreenW-39*2 - 160)/3;
    
    UIButton *buton1  = [UIButton setButtonWithFrame:CGRectMake(jianju,jizhubutt.bottom+16, 80, 25) Title:@"放弃卖号" Image:@"" Font:12 Color:RGB(255, 255, 255) Target:self Selector:@selector(bu1Tap:)];
    buton1.backgroundColor = RGB(26, 113, 255);
    buton1.layer.cornerRadius = 3;
    buton1.layer.masksToBounds = YES;
    
    [self.fenxiangView addSubview:buton1];
    
    
    
    UIButton *buton2  = [UIButton setButtonWithFrame:CGRectMake(jianju*2+80,jizhubutt.bottom+16, 80, 25) Title:@"确认出售" Image:@"" Font:12 Color:RGB(255, 255, 255) Target:self Selector:@selector(bu2Tap:)];
    buton2.layer.cornerRadius = 3;
    buton2.layer.masksToBounds = YES;
    buton2.backgroundColor = RGB(26, 113, 255);
    [self.fenxiangView addSubview:buton2];
    
    self.fenxiangView.frame = CGRectMake(39, (kScreenH-buton2.bottom-15)/2, kScreenW-39*2,buton2.bottom+15);
    
    
    
    
    
}



- (void)bu1Tap:(UIButton *)sender {
    
    [self.delegate HWSellTopViewGiveUpToBuy];
    
    
}

-(void)bu2Tap:(UIButton *)sender {
    
    if ( self.jizhubutt.selected) {
        
        [self.delegate HWSellTopViewBuy];
        
        
    } else {
        
        [MBProgressHUD showError:@"你还没有同意用户协议那"];
    }
    
}








// 用户协议是否同意
- (void)xieYiButtonTap:(UIButton *)sender {
    
    if (sender.selected == YES) {
        
        sender.selected = NO;
        
    } else {
        
        sender.selected = YES;
        
        
    }
    
    
    
}


- (void)addmaijiaxuezhshouhu {
    
    UILabel *label  = [[UILabel alloc]initWithFrame:CGRectMake(0, 15, kScreenW-39*2, 14)];
    label.numberOfLines = 0;
    NSMutableAttributedString *string = [[NSMutableAttributedString alloc] initWithString:@"卖家必读" attributes:@{NSFontAttributeName: [UIFont fontWithName:@"PingFang SC" size: 14],NSForegroundColorAttributeName: [UIColor colorWithRed:51/255.0 green:51/255.0 blue:51/255.0 alpha:1.0]}];
    label.attributedText = string;
    label.textAlignment = NSTextAlignmentCenter;
    [self.chushouView  addSubview:label];
    
    
    NSArray *arr = @[@"1.出售申请提交后，官方将冻结小号审核信息，该小号在该游戏中的角色都将移除，无法登陆；",@"2.审核小号信息需保证真实有效，若审核不通过，或者您下架商品，可重新登录小号；",@"3.一旦售出，无法找回小号；",@"4.售出将收取5%手续费（最低5元），月度会员只需收取4%手续费（最低4元），季度会员只需收取3%手续费，年度会员免手续费！剩余收益发送至您的钱包内。",@"5.商品售出后，买家3天内无投诉，收益可解冻提现。"];
    
    NSMutableArray  *arrH =  [[NSMutableArray alloc]init];
    for (int i = 0 ; i < arr.count; i++) {
        CGFloat h =  [self  calculateRowHeight:arr[i] fontSize:12];
        [arrH addObject:[NSString stringWithFormat:@"%f",h]];
        
        
    }
    
    for (int i = 0 ; i < arr.count; i++) {
        
        UILabel *label = [[UILabel alloc] init];
        label.numberOfLines = 0;
        label.font = [UIFont systemFontOfSize:12];
        label.textColor = RGB(51, 51, 51);
        
        label.text = arr[i];
        
        label.frame = CGRectMake(17, 0, kScreenW-17*2, 30);
        [self.chushouView addSubview:label];
        
        if (i == 0) {
            CGFloat h0 = [arrH[0] floatValue];
            
            label.frame = CGRectMake(21, 44, kScreenW-21*2-39*2, h0);
            
            
        } else if (i == 1) {
            CGFloat h0 = [arrH[0] floatValue];
            CGFloat h1 = [arrH[1] floatValue];
            label.frame = CGRectMake(21,44+h0+i*10, kScreenW-21*2-39*2, h1);
            
        } else if (i == 2){
            
            CGFloat h0 = [arrH[0] floatValue];
            CGFloat h1 = [arrH[1] floatValue];
            CGFloat h2 = [arrH[2] floatValue];
            label.frame = CGRectMake(21,44+ h0+h1+i*10,kScreenW-21*2-39*2, h2);
            
            
        }  else if (i == 3){
            
            CGFloat h0 = [arrH[0] floatValue];
            CGFloat h1 = [arrH[1] floatValue];
            CGFloat h2 = [arrH[2] floatValue];
            CGFloat h3 = [arrH[3] floatValue];
            label.frame = CGRectMake(21,44+ h0+h1+h2+i*10, kScreenW-21*2-39*2, h3);
//
//            NSMutableAttributedString *atrtabl =  [WMUtil   attributedStringWithText:arr[i] withRangText:@"4.售出将收取5%手续费（最低5元）" withColor:RGB(236, 11, 11) withFont:[UIFont systemFontOfSize:12]];
            
 //           label.attributedText = atrtabl;
            
            
        } else {
            
            CGFloat h0 = [arrH[0] floatValue];
            CGFloat h1 = [arrH[1] floatValue];
            CGFloat h2 = [arrH[2] floatValue];
            CGFloat h3 = [arrH[3] floatValue];
            CGFloat h4 = [arrH[4] floatValue];
            label.frame = CGRectMake(21, 44+h0+h1+h2+h3+i*10, kScreenW-21*2-39*2, h4);
//            NSMutableAttributedString *atrtabl =  [WMUtil   attributedStringWithText:arr[i] withRangText:@"买家3天内无投诉" withColor:RGB(236, 11, 11) withFont:[UIFont systemFontOfSize:12]];
//            label.attributedText = atrtabl;
            self.chushouView.frame = CGRectMake(0,0, kScreenW, 44+h0+h1+h2+h3+h4+i*10);
            
            
        }
        
        
    }
    
}













// 添加灰色View
-(void)addgrayView {
    
    self.grayView = [[UIView alloc]init];
    self.grayView.frame  = CGRectMake(0,0, kScreenW, kScreenH);
    
    self.grayView.backgroundColor =  RGB(51, 51, 51);
    self.grayView.alpha = 0.8;
    [self addSubview:self.grayView];
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapView:)];
    //  tap.delegate = self;
    [self.grayView addGestureRecognizer:tap];
    
    
    
    
}



- (void)tapView:(UITapGestureRecognizer *)touch {
//    
//    [touch.view.superview removeFromSuperview];
//    
    
    
}



- (CGFloat)calculateRowHeight:(NSString *)string fontSize:(NSInteger)fontSize

{
    
    NSDictionary *dic = @{NSFontAttributeName:[UIFont systemFontOfSize:fontSize]};
    
    //  NSDictionary *dic = @{NSFontAttributeName:[UIFont fontWithName:@"PingFang SC" size: 12]};
    
    CGRect rect = [string boundingRectWithSize:CGSizeMake(kScreenW-60*2, MAXFLOAT)/*计算高度要先指定宽度*/ options:NSStringDrawingUsesLineFragmentOrigin |
                   
                   NSStringDrawingUsesFontLeading attributes:dic context:nil];
    
    return rect.size.height;
    
}




@end
